package com.base.platform.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * @Class ResponseResult
 * @Description response返回结果封装
 * @Author lihuibin
 * @Date 2020/12/16 22:12
 * @Version 1.0
 * 更新记录:
 * 更新时间        更新人        更新位置        更新内容
 * 2020/12/16       lihuibin       新建           新建
 */
@Data
public class ResponseResult implements Serializable {
    private int code;
    private String msg;
    private Object data;


    private ResponseResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResponseResult success(){
        return new ResponseResult(CODE.SUCCESS.getCode(),CODE.SUCCESS.getMsg(),null);
    }

    public static ResponseResult success(Object data) {
        return new ResponseResult(CODE.SUCCESS.getCode(),CODE.SUCCESS.getMsg(),data);
    }

    public static ResponseResult success(String msg, Object data) {
        return new ResponseResult(CODE.SUCCESS.getCode(),msg,data);
    }

    public static ResponseResult fail(int code, String msg, Object data) {
        return new ResponseResult(code + CODE.FAIL.getCode(),msg,data);
    }

    public static ResponseResult fail(String msg, Object data) {
        return new ResponseResult(CODE.FAIL.getCode(),msg,data);
    }

    public static ResponseResult fail(Object data) {
        return new ResponseResult(CODE.FAIL.getCode(),CODE.FAIL.getMsg(),data);
    }

    public static ResponseResult error(int code, String msg, Object data) {
        return new ResponseResult(code + CODE.SERVER_ERROR.getCode(),msg,data);
    }

    public static ResponseResult error(String msg, Object data) {
        return new ResponseResult(CODE.SERVER_ERROR.getCode(),msg,data);
    }

    public static ResponseResult error(Object data) {
        return new ResponseResult(CODE.SERVER_ERROR.getCode(),CODE.SERVER_ERROR.getMsg(),data);
    }


    public enum CODE {
        /** 成功---200 */
        SUCCESS(200,"OK"),
        /** 请求成功的业务失败状态码前缀 */
        FAIL(2000,"fail"),
        /** 参数校验状态码前缀 */
        PARAMS_VALIDATE(3000,"fail"),
        /** 服务器内部错误 */
        SERVER_ERROR(500,"server internal error"),
        /** 资源找不到 */
        RESOURCE_NOT_FOUND(404,"resource not found")
        ;
        private int code;
        private String msg;

        CODE(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public String getMsg() {
            return msg;
        }
    }


}
